package com.yunfei.api.financialCalculator.service;

import com.yunfei.api.financialCalculator.bo.FcjsqHouseBaseInfo;
import com.yunfei.api.financialCalculator.bo.FcjsqHouseCity;
import com.yunfei.api.financialCalculator.bo.HouseArea;
import com.yunfei.api.financialCalculator.contants.CalculatorContants;
import com.yunfei.api.financialCalculator.param.FangChanCalParam;
import com.yunfei.api.financialCalculator.param.FangChanCalProcedureParam;
import com.yunfei.api.financialCalculator.param.HouseAreaParam;
import com.yunfei.api.financialCalculator.result.FangChanCalResult;
import com.yunfei.bo.result.ResultModel;
import com.yunfei.enums.GuDingShouYiCalResultStatus;
import com.yunfei.enums.ResultStatus;
import com.yunfei.mapper.aiiidb.HourseCityMapper2;
import com.yunfei.procedure.FangChanCalProcedure;
import com.yunfei.utils.jdbc.AiiidbUtil;
import com.yunfei.utils.string.StringUtil;
import org.springframework.core.env.Environment;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class FangChanCalService {
    //房产计算器接口
    public static ResultModel cal(FangChanCalParam param, Environment env, HourseCityMapper2 hourseCityMapper){
        ResultModel resultModel=new ResultModel();
        Map<String,Object> queryMap=new HashMap<>();
        try{
            String city=param.getCity();//省市
            String area=param.getArea();//区县
            String street=param.getStreet();//街道
            String square=param.getSquare();//面积
            //查询结果
            queryMap.put("city",city);
            queryMap.put("area",area);
            queryMap.put("street",street);
            FcjsqHouseBaseInfo fcjsqHouseBaseInfo=hourseCityMapper.get(queryMap);
            String houseValue="";//房产价值
            String leaseValue="";//租房价值
            if(!StringUtil.isNullOrEmpty(fcjsqHouseBaseInfo)){
                String leasePrice=fcjsqHouseBaseInfo.getLeasePrice();//房租价格 每平米 输入平米后计算租房价值
                String avgSalePrice=fcjsqHouseBaseInfo.getAvgSalePrice();//房屋价格 每平米 输入平米后计算房产价值
                houseValue=Double.parseDouble(avgSalePrice)*Double.parseDouble(square)+"";
                leaseValue=Double.parseDouble(leasePrice)*Double.parseDouble(square)+"";
            }
            //查询利率
            String rate=hourseCityMapper.getRate();
            FangChanCalProcedureParam fangChanCalProcedureParam=new FangChanCalProcedureParam();
            fangChanCalProcedureParam.setHouseValue(houseValue);
            fangChanCalProcedureParam.setLeaseValue(leaseValue);
            fangChanCalProcedureParam.setRate(rate);
            fangChanCalProcedureParam.setYearCnt(CalculatorContants.YEAR_CNT);
            //调用存储过程
            FangChanCalResult result=new FangChanCalProcedure().callProcedure(AiiidbUtil.getInstance(env),fangChanCalProcedureParam);
            result.setHouseValue(houseValue);
            result.setLeaseValue(leaseValue);
            resultModel.setData(result);
            resultModel.setCode(ResultStatus.SUCCESS.getCode());
            resultModel.setMessage(ResultStatus.SUCCESS.getMessage());
        }catch (Exception e){
            e.printStackTrace();
        }
        return resultModel;
    }
    public static ResultModel getAreaOrStreet(HouseAreaParam param, Environment env, HourseCityMapper2 hourseCityMapper){
        ResultModel resultModel=new ResultModel();
        List<HouseArea> citys=null;
        Map<String,Object> queryMap=new HashMap<>();
        try{
            String depth=param.getDepth();//节点深度
            String name=param.getName();//节点名称
            if(StringUtil.isNullOrEmpty(depth)){
                resultModel.setCode(GuDingShouYiCalResultStatus.depth_notnull.getCode());
                resultModel.setMessage(GuDingShouYiCalResultStatus.depth_notnull.getMessage());
                return resultModel;
            }else{
                if(!CalculatorContants.GDSY_QUERY_CITY_DEPTH_1.equals(depth)&&!CalculatorContants.GDSY_QUERY_CITY_DEPTH_2.equals(depth)&&!CalculatorContants.GDSY_QUERY_CITY_DEPTH_3.equals(depth)){
                    resultModel.setCode(GuDingShouYiCalResultStatus.depth_error.getCode());
                    resultModel.setMessage(GuDingShouYiCalResultStatus.depth_error.getMessage());
                    return resultModel;
                }
            }

            queryMap.put("label",name);
            if(CalculatorContants.GDSY_QUERY_CITY_DEPTH_1.equals(depth)){
                //查询省市
                citys=hourseCityMapper.getCity(queryMap);
            }
            if(CalculatorContants.GDSY_QUERY_CITY_DEPTH_2.equals(depth)){
                if(StringUtil.isNullOrEmpty(name)){
                    resultModel.setCode(GuDingShouYiCalResultStatus.name_parent_city_notnull.getCode());
                    resultModel.setMessage(GuDingShouYiCalResultStatus.name_parent_city_notnull.getMessage());
                    return resultModel;
                }
                //查询省市
                citys=hourseCityMapper.getAreaByCity(queryMap);
            }
            if(CalculatorContants.GDSY_QUERY_CITY_DEPTH_3.equals(depth)){
                if(StringUtil.isNullOrEmpty(name)){
                    resultModel.setCode(GuDingShouYiCalResultStatus.name_parent_area_notnull.getCode());
                    resultModel.setMessage(GuDingShouYiCalResultStatus.name_parent_area_notnull.getMessage());
                    return resultModel;
                }
                //查询省市
                citys=hourseCityMapper.getStreetByArea(queryMap);
            }
            resultModel.setData(citys);
            resultModel.setCode(ResultStatus.SUCCESS.getCode());
            resultModel.setMessage(ResultStatus.SUCCESS.getMessage());
        }catch (Exception e){
            e.printStackTrace();
        }
        return resultModel;
    }
    public static ResultModel getCity(HourseCityMapper2 hourseCityMapper){
        ResultModel resultModel=new ResultModel();
        List<FcjsqHouseCity> citys=null;
        try{
            citys=hourseCityMapper.getCitys(new HashMap<>());
            for(int i=0;i<citys.size();i++){
                String city=citys.get(i).getLabel();
                Map<String,Object> queryMap=new HashMap<>();
                queryMap.put("label",city);
                List<HouseArea> areas=hourseCityMapper.getAreaByCity(queryMap);
                if(!StringUtil.isNullOrEmpty(areas)&&areas.size()>0){
                    citys.get(i).setChildren(areas);
                }
            }
            resultModel.setData(citys);
            resultModel.setCode(ResultStatus.SUCCESS.getCode());
            resultModel.setMessage(ResultStatus.SUCCESS.getMessage());
        }catch (Exception e){
            e.printStackTrace();
        }
        return resultModel;
    }
}
